import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DamengJdbcExample {

    public static void main(String[] args) {
        // 数据库URL，格式为 jdbc:dm://<hostname>:<port>/<database>
        String url = "jdbc:dm://localhost:5236/DAMENG";
        // 数据库用户名
        String user = "username";
        // 数据库密码
        String password = "password";

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            // 加载驱动程序
            Class.forName("dm.jdbc.driver.DmDriver");

            // 建立连接
            connection = DriverManager.getConnection(url, user, password);
            System.out.println("成功连接到达梦数据库");

            // 创建Statement对象
            statement = connection.createStatement();

            // 执行查询
            String sql = "SELECT * FROM your_table";
            resultSet = statement.executeQuery(sql);

            // 处理结果集
            while (resultSet.next()) {
                System.out.println("列1: " + resultSet.getString("column1"));
                System.out.println("列2: " + resultSet.getString("column2"));
            }

        } catch (ClassNotFoundException e) {
            System.err.println("找不到JDBC驱动程序类");
            e.printStackTrace();
        } catch (SQLException e) {
            System.err.println("数据库连接失败");
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (resultSet != null) resultSet.close();
                if (statement != null) statement.close();
                if (connection != null) connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
